/*
 * Utility for reading files in binary form.
 */
package sopa.java.io;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

public class Files
{
    public static byte[] readBinary(String resource) throws IOException
    {
        InputStream s = Files.class.getResourceAsStream(resource);
        if (s == null)
        {
            return null;
        }
        final BufferedInputStream bf = new BufferedInputStream(s);
        try
        {
            final byte[] data = new byte[bf.available()];
            bf.read(data);
            return data;
        } finally
        {
            bf.close();
        }
    }
    
    public static byte[] readBinary(File file) throws IOException
    {
        final BufferedInputStream bf = new BufferedInputStream(new FileInputStream(file));
        try
        {
            final byte[] data = new byte[bf.available()];
            bf.read(data);
            return data;
        } finally
        {
            bf.close();
        }
    }

    /**
     * Reads a text file as a single string.
     * 
     * @throws IOException
     */
    public static String readText(File file) throws IOException
    {
        StringBuilder sb = new StringBuilder();
        BufferedReader in = new BufferedReader(new FileReader(file.getAbsoluteFile()));
        try
        {
            String s;
            while ((s = in.readLine()) != null)
            {
                sb.append(s);
                sb.append("\n");
            }
        } finally
        {
            in.close();
        }
        return sb.toString();
    }
    
    /**
     * Reads a text file with given name as a single string.
     * 
     * @throws IOException
     */
    public static String readText(String fileName) throws IOException
    {
        return readText(new File(fileName).getAbsoluteFile());
    }

    /**
     * Reads a text file to a List of Strings.
     * 
     * @throws IOException
     */
    public static List<String> readTextToList(String fileName, String splitter) throws IOException
    {
        List<String> list = Arrays.asList(readText(fileName).split(splitter));
        // Regular expression split() often leaves an empty String at the first
        // position.
        if (list.get(0).equals(""))
        {
            list.remove(0);
        }
        return list;
    }

    /**
     * Reads a text file, by lines, to a list of Strings.
     * 
     * @throws IOException
     */
    public static List<String> readTextToList(String fileName) throws IOException
    {
        return readTextToList(fileName, "\n");
    }

    /**
     * Writes a String to a text file.
     * 
     * @throws IOException
     */
    public static void writeText(String fileName, String text) throws IOException
    {
        PrintWriter out = new PrintWriter(new File(fileName).getAbsoluteFile());
        try
        {
            out.print(text);
        } finally
        {
            out.close();
        }
    }

    /**
     * Writes a list of Strings to a text file.
     * 
     * @throws IOException
     */
    public static void writeText(String fileName, List<String> stringList) throws IOException
    {
        PrintWriter out = new PrintWriter(new File(fileName).getAbsoluteFile());
        try
        {
            for (String item : stringList)
                out.println(item);
        } finally
        {
            out.close();
        }
    }
}
